package com.example.flag03;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextPaint;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;

public class detail extends AppCompatActivity {

    //    展示数据在数据库中的_id
    private int _id = 0;
    //    数据库变量
    private MyDatabaseHelper databaseHelper;
    //    数据库实体游标
    private Cursor cursor;
    //    控件变量
    private String signal = null;
    private String feelText=null;
//        当前时间
    private int nowYear;
    private int nowMonth;
    private int nowDay;
    private boolean isTrue=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detial);

//        接收传递过来的_id
        Intent intent = getIntent();
        _id = intent.getIntExtra("_id", 0);
        nowYear = intent.getIntExtra("nowYear",0);
        nowMonth = intent.getIntExtra("nowMonth",0);
        nowDay = intent.getIntExtra("nowDay",0);
        Toast.makeText(detail.this, "" + _id, Toast.LENGTH_SHORT).show();
        isTrue=false;

//        加粗文本
        setBold();

//       根据传来的_id查询数据
        getData();

//        修改完成状态
        alterSignal();

//        写感想
        writeFeel();

//       保存修改，上传数据
        sendData();
    }


    //        保存修改，上传数据
    private void sendData() {
        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText textView=(EditText) findViewById(R.id.feelText);
                if(textView.getText().toString()!=null){
                    feelText=textView.getText().toString();
                }
                SQLiteDatabase db = databaseHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                if(feelText!=null) {
                    values.put("feel", feelText);
                }
                values.put("signal", signal);
                if(signal.equals("1")&&isTrue) {
                    values.put("finishedYear", nowYear);
                    values.put("finishedMonth", nowMonth);
                    values.put("finishedDay", nowDay);
                }
                db.update("Flag",values,"_id="+_id,null);
                Toast.makeText(detail.this, "保存成功！", Toast.LENGTH_SHORT).show();
                textView.setFocusable(false);
            }
        });
        databaseHelper.close();
    }

    //        写感想
    private void writeFeel() {
        ImageView imageView=(ImageView)findViewById(R.id.feelImage);
        EditText textView=(EditText) findViewById(R.id.feelText);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setFocusableInTouchMode(true);
                textView.setHint("请输入你的感想！");
            }
        });
    }

    //        修改完成状态
    private void alterSignal() {
        ImageView imageView = (ImageView) findViewById(R.id.signalImage);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (signal) {
                    case "-1":
                        tip1("已过期！");
                        break;
                    case "0":
                        tip2();
                        break;
                    case "1":
                        tip1("已完成！");
                        break;
                }
            }
        });
    }

    //    提示框1
    private void tip1(String string) {
        //        弹窗提示
        final AlertDialog.Builder builder = new AlertDialog.Builder(detail.this);
        builder.setTitle("提示");
        builder.setCancelable(false);
        builder.setMessage(string);
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    //    提示框2
    private void tip2() {
        //        弹窗提示
        final AlertDialog.Builder builder = new AlertDialog.Builder(detail.this);
        builder.setTitle("修改状态");
        builder.setCancelable(false);
        builder.setMessage("是否已完成该计划？");
        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                signal = "1";
                ImageView imageView = (ImageView) findViewById(R.id.signalImage);
                TextView textView = (TextView) findViewById(R.id.signalText);
                imageView.setImageResource(R.drawable.correct);
                textView.setText("已完成！");
                isTrue=true;
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }


    //    查询数据,并赋值
    private void getData() {
        //创建数据库的对象
        databaseHelper = new MyDatabaseHelper(this, "FlagData.dp", null, 1);
        //读取数据库
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
//        查询数据库
        cursor = db.query("Flag", null, "_id=" + _id, null, null, null, null);

        String flagTitle = null;
        String flagPlan = null;
        String fromTime = null;
        String toTime = null;

        if (cursor.moveToFirst()) {
            flagTitle = cursor.getString(cursor.getColumnIndex("flagTitle"));
            flagPlan = cursor.getString(cursor.getColumnIndex("flagPlan"));
            signal = cursor.getString(cursor.getColumnIndex("signal"));
            fromTime = cursor.getString(cursor.getColumnIndex("fromYear")) + "." +
                    cursor.getString(cursor.getColumnIndex("fromMonth")) + "." +
                    cursor.getString(cursor.getColumnIndex("fromDay")) +"~";
            toTime = cursor.getString(cursor.getColumnIndex("toYear")) + "." +
                    cursor.getString(cursor.getColumnIndex("toMonth")) + "." +
                    cursor.getString(cursor.getColumnIndex("toDay")) ;
            feelText=cursor.getString(cursor.getColumnIndex("feel"));
        }

//        查找控件的_id
        TextView titleText = findViewById(R.id.titleText);
        TextView planText = findViewById(R.id.planText);
        TextView timeText = findViewById(R.id.timeText);
        EditText editText = findViewById(R.id.feelText);
//        赋值
        titleText.setText(flagTitle);
        planText.setText(flagPlan);
        timeText.setText(fromTime + toTime);
        if(feelText!=null){
            editText.setText(feelText);
        }

//        更改图标,文字
        ImageView imageView = (ImageView) findViewById(R.id.signalImage);
        TextView textView = (TextView) findViewById(R.id.signalText);
        switch (signal) {
            case "-1":
                imageView.setImageResource(R.drawable.out);
                textView.setText("未完成！");
                break;
            case "0":
                imageView.setImageResource(R.drawable.wait);
                textView.setText("待完成！");
                break;
            case "1":
                imageView.setImageResource(R.drawable.correct);
                textView.setText("已完成！");
                break;
        }
    }

    //        加粗文本
    private void setBold() {
        TextView textView1 = findViewById(R.id.flagTitle);
        TextView textView2 = findViewById(R.id.flagPlan);
        TextView textView3 = findViewById(R.id.timeTitle);
        TextView textView4 = findViewById(R.id.feelTitle);
        TextPaint tp1 = textView1.getPaint();
        tp1.setFakeBoldText(true);
        TextPaint tp2 = textView2.getPaint();
        tp1.setFakeBoldText(true);
        TextPaint tp3 = textView3.getPaint();
        tp1.setFakeBoldText(true);
        TextPaint tp4 = textView4.getPaint();
        tp1.setFakeBoldText(true);
    }
}